import java.io.*;
import java.net.*;
import java.lang.Thread;

class WorkerThread implements Runnable
{
  Socket socket;
  BufferedReader myReader;
  PrintWriter myWriter;

  public WorkerThread( Socket passedSocket ) throws IOException
  {
    //System.out.println("clientSocket : " + clientSocket);      
    socket = passedSocket;
    myReader = new BufferedReader(new InputStreamReader( socket.getInputStream()));
    myWriter = new PrintWriter(new OutputStreamWriter( socket.getOutputStream()));
  }

  public void run()
  {
	String line;
    System.out.println("Thread Listening on new socket : " + socket);
    
    while(true)   
    {
	 //System.out.println("Entering infinite loop\n");
	
	try{
		String sentText = new String(myReader.readLine());
		if(sentText != null)
		{		
          System.out.println(sentText);
        }else{}

    	}catch( IOException e){}
         catch( NullPointerException ne){}
	}		
  }// End run
}// End class workerThread()


public class Listener{
	public static void main(String args[]) throws IOException
	{
		Integer portNumber = new Integer(15687);
        
        try{
		    ServerSocket serverSocket = new ServerSocket( portNumber );
		    Socket socket = new Socket();
		      while(true)
	          {
	             System.out.println("\nServer Listening on port # :" + portNumber);
	             socket = serverSocket.accept();
				 WorkerThread worker = new WorkerThread(socket);
				 Thread workerThread = new Thread(worker);
				 workerThread.start();
		      }
			}catch( IOException e)
			 {
                System.out.println(e);        
             }
		
	}// End main
	
}// End class server()
